$.extend(planner.roster.ui,
{
    setup_buttons: function ()
    {
        $("#btnTakeLead").button({
            icons: {
                primary: "ui-icon-star"
            }
        }).click(function(caller, x) {
            console.log("#btnTakeLead_click");
            
            planner.server.post_mech(
                { i:"1", a:"l", players: planner.mech.player },
                function (html) {
                    planner.mech.ui.update();
                    
                    if ( planner.roster.ui.is_visible() )
                    {
                        planner.roster.ui.update_html(html);
                        planner.roster.show();
                    }
                    
                },
                function (html) { },
                false
            );    
        }).tooltip({ show: { delay: 1000 } });
          
        $("#btnRosterView")
        .button({
            text: false,
            icons: {
                primary: "ui-icon-newwin",
                secondary: "ui-icon-triangle-1-s"
            }
        }).click(function(event){
            event.stopPropagation();
            $('#mnuRosterView').css("display","block");
            $('#mnuAssignLance').css("display","none");
            $('#mnuRosterView').menu({ position: { my: "left top", at: "left bottom", of: this } }).show()
            var menu = $('#mnuRosterView').menu().show().position({
                my: "left top",
                at: "left bottom",
                of: this
            });
            
            $('body').one("click", function() {  $('#mnuRosterView').css("display","none"); })
        }).tooltip({ show: { delay: 1000 } }).hide();
        
        $("#btnToggleRosterViewTable")
        .button({
            text: false,
            icons: {
                primary: "ui-icon-newwin",
            }
        }).click(function(event){
            $("#btnToggleRosterViewGrid").show();
            $("#btnToggleRosterViewTable").hide();
            planner.roster.lastView = "table";
            $("#tblRoster").css("display", "block");    
            $("#gridRoster").css("display", "none");  
        }).dblclick(function(){}).tooltip({ show: { delay: 1000 } });
        
        $("#btnToggleRosterViewGrid")
        .button({
            text: false,
            icons: {
                primary: "ui-icon-newwin",
            }
        }).click(function(event){
            $("#btnToggleRosterViewGrid").hide();
            $("#btnToggleRosterViewTable").show();
            
            planner.roster.lastView = "grid";
            $("#tblRoster").css("display", "none");    
            $("#gridRoster").css("display", "block");
        }).dblclick(function(){}).tooltip({ show: { delay: 1000 } });

        if ( planner.roster.lastView == "grid" )
        {
            $("#btnToggleRosterViewGrid").hide();
        }
        else
        {
            $("#btnToggleRosterViewTable").hide();

        }
        
        
        $("#btnRosterViewTable").click(function(event) {
            planner.roster.lastView = "table"
            $("#tblRoster").css("display", "block");    
            $("#gridRoster").css("display", "none");    
        });
        $("#btnRosterViewGrid").click(function(event) {
            planner.roster.lastView = "grid"
            $("#tblRoster").css("display", "none");    
            $("#gridRoster").css("display", "block");
        });
        
        $("#btnImportFromSmurfy").button({
            icons: {
                 primary: "ui-icon-link"
            }
         }).click(function(){
            planner.dialogs.importMech.show();
         }).tooltip({ show: { delay: 1000 } });
        
        $("#uisave").button({
            icons: {
                primary: "ui-icon-disk"
            }
         }).click(function(caller, x) {
            planner.dialogs.saveMech.show();
            
        }).tooltip({ show: { delay: 1000 } });
        
        $("#btnGarageOf").button({
            text: false,
            icons: {
                primary: "ui-icon-calculator"
            }
        }).click(function(event){
            event.stopPropagation();
            $(".post").hide();
            planner.garage.ui.update();
        }).tooltip({ show: { delay: 1000 } });
                
        $("#btnRosterSort").button({
            text: false,
            icons: {
                primary: "ui-icon-shuffle",
                secondary: "ui-icon-triangle-1-s"
            }
        }).click(function()
        {
            event.stopPropagation();
            $('#mnuRosterSort').css("display","block");
            $('#mnuRosterSort').menu({ position: { my: "left top", at: "left bottom", of: this } }).show()
            var menu = $('#mnuRosterSort').menu().show().position({
                my: "left top",
                at: "left bottom",
                of: this
              });
            
            $('body').one("click", function() {  $('#mnuRosterSort').css("display","none"); })

        }).tooltip({ show: { delay: 1000 } });
        
        $("#sortbylance").click(function(event) { planner.roster.ui.sort_table($("#uisortlance")); $('#mnuRosterSort').hide() });
        $("#sortbylance").dblclick(function(event) { planner.roster.ui.sort_table($("#uisortlance")); $('#mnuRosterSort').hide() });

        $("#sortbyname").click(function(event) { planner.roster.ui.sort_table($("#uisortplayer")); $('#mnuRosterSort').hide() });
        $("#sortbyname").dblclick(function(event) { planner.roster.ui.sort_table($("#uisortplayer")); $('#mnuRosterSort').hide() });
        
        $("#sortbymech").click(function(event) { planner.roster.ui.sort_table($("#uisortmech")); $('#mnuRosterSort').hide() });
        $("#sortbymech").dblclick(function(event) { planner.roster.ui.sort_table($("#uisortmech")); $('#mnuRosterSort').hide() });

        $("#sortbyspeed").click(function(event) { planner.roster.ui.sort_table($("#uisortspeed")); $('#mnuRosterSort').hide() });
        $("#sortbyspeed").dblclick(function(event) { planner.roster.ui.sort_table($("#uisortspeed")); $('#mnuRosterSort').hide() });
        
        $("#sortbyweight").click(function(event) { planner.roster.ui.sort_table($("#uisortweight")); $('#mnuRosterSort').hide() });
        $("#sortbyweight").dblclick(function(event) { planner.roster.ui.sort_table($("#uisortweight")); $('#mnuRosterSort').hide() });
        
   
        $("#btnAssignLance")
        .button({
            text: false,
           
            icons: {
               
                primary: "ui-icon-wrench",
                secondary: "ui-icon-triangle-1-s"
            }
        }).click(function(event){
            event.stopPropagation();
            $('#mnuAssignLance').css("display","block");
            $('#mnuRosterView').css("display","none");
             
            $('#mnuAssignLance').menu({ position: { my: "left top", at: "left bottom", of: this } }).show()
            var menu = $('#mnuAssignLance').menu().show().position({
                my: "left top",
                at: "left bottom",
                of: this
            });
            
            $('body').one("click", function() {  $('#mnuAssignLance').css("display","none"); })
        }).dblclick(function(event){
            event.stopPropagation();
        }).tooltip({ show: { delay: 1000 } }).hide();
        //alert("a");
        
        rosterUpdateCallback = function() {
            //planner.roster.ui.update();
            //setTimeout(function(){
            //    planner.roster.ui.update(function(){ $("#roster").show(); $("#rostercontent").show(duration=0) });
            //}, 100);
            
            //    planner.roster.ui.auto_update(function(){ $("#roster").show(); $("#rostercontent").show(duration=0); planner.drop.ui.remove_selections(); })
            //}, 100);
        };
    
        $("#btnRemovePlayer").click(function(event) {
            console.log("#btnRemovePlayer_click");
            
            $.each(planner.roster.selected_players_names(), function(index, name) {
                if (planner.mech.player == name)        
                {
                    planner.server.post_mech( 
                        { players: planner.mech.player, a: "x" }, 
                        function(data) { location.href='/'; },
                        function() { location.href='/';},
                        true
                    );
                }
            });

            $.each(planner.roster.selected_players_names(), function(index, name) {
                if (planner.mech.player != name)        
                {
                    event.stopPropagation();
                    $("#btnAssignLance").hide();
                    planner.roster.ui.post_action("x", rosterUpdateCallback)
                }
            });
            
        });


        $("#btnAssignLanceAlpha").click(function(event) { event.stopPropagation(); planner.roster.ui.post_action("a", rosterUpdateCallback); });
        $("#btnAssignLanceBravo").click(function(event) { event.stopPropagation(); planner.roster.ui.post_action("b", rosterUpdateCallback) });
        $("#btnAssignLanceCharlie").click(function(event) { event.stopPropagation(); planner.roster.ui.post_action("c", rosterUpdateCallback) });
        $("#btnAssignLanceDelta").click(function(event) { event.stopPropagation(); planner.roster.ui.post_action("d", rosterUpdateCallback) });
        $("#btnAssignReserve").click(function(event) { event.stopPropagation(); planner.roster.ui.post_action("r", rosterUpdateCallback) });
        
        $("#btnAssignLanceAlpha").dblclick(function(event) { event.stopPropagation(); planner.roster.ui.post_action("a", rosterUpdateCallback); });
        $("#btnAssignLanceBravo").dblclick(function(event) { event.stopPropagation(); planner.roster.ui.post_action("b", rosterUpdateCallback) });
        $("#btnAssignLanceCharlie").dblclick(function(event) { event.stopPropagation(); planner.roster.ui.post_action("c", rosterUpdateCallback) });
        $("#btnAssignLanceDelta").dblclick(function(event) { event.stopPropagation(); planner.roster.ui.post_action("d", rosterUpdateCallback) });
        $("#btnAssignReserve").dblclick(function(event) { event.stopPropagation(); planner.roster.ui.post_action("r", rosterUpdateCallback) });
        
        
    
    },
    set_button_state: function()
    {
        console.log("planner.roster.ui.set_button_state(): selections: " + planner.roster.ui.has_selections());
        if ( planner.roster.ui.has_selections() )
            planner.roster.ui.enable_buttons_that_need_selection();
        else
            planner.roster.ui.disable_buttons_that_need_selection();
    },
    has_selections: function()
    {
        x = false;
        
        $(".cell-roster-grid").each(function() {
            if ( $(this).hasClass("gradient") )
                x = true;
        });
        
        $(".row-roster-table").each(function() {
            if ( $(this).hasClass("gradient") )
                x = true;
        });

        console.log("planner.roster.ui.has_selections(" + x + ")");
        
        return x;
    },
    setup_views: function () {
        console.log("planner.roster.ui.setup_views()");
        $(".cell-roster-grid").hover( 
            function() {
                planner.drop.ui.select_div( $(this), false, true );
            },
            function() {
                planner.drop.ui.deselect_div( $(this), false, true );
            }
        );
        
        $(".row-roster-table").tooltip({ show: { delay: 2000 } });
        $(".cell-roster-grid").tooltip({ show: { delay: 2000 } });

        $(".row-roster-table").click( function(event) {
            event.stopPropagation();
            element = $(this);
            planner.drop.ui.toggle_selection(element);
            planner.roster.ui.set_button_state();
        });
        
        $(".row-roster-table").dblclick( function(event) {
            event.stopPropagation();
            element = $(this);
            planner.drop.ui.toggle_selection(element);
            planner.roster.ui.set_button_state();
        });
        
        
        
        $(".cell-roster-grid").click( function() {
            
            event.stopPropagation();
            //alert("X");           
            $(this).css("border", "solid #ccc 2px");
            $(this).css("margin", "4px");
            //{% if starter != mech.player %}
            $(".cell-roster-grid").removeClass("gradient");
            //{% endif %}
            if ( $(this).hasClass("gradient") )
            {
                $(this).removeClass("gradient");
                $(".cell-roster-grid").css("border", "");
                $(".cell-roster-grid").css("margin", "6px");
            }
            else    
            {
                //alert("XX");    
                $(this).addClass("gradient");
            }
                
            planner.roster.ui.set_button_state();
        });
        
        $(".uisort").hover(
            function() {
                $(this).css("text-decoration", "underline");
            },
            function() {
                $(this).css("text-decoration", "none");
            }
        );
            
        $('.uisort')
            .wrapInner('<span title="sort this column"/>')
            .each(function(){
                
                var th = $(this);
                
                th.click(function(event){
                    event.stopPropagation();
                    planner.roster.ui.sort_table($(this));
                });
                    
            });
        
        remove_ui_selections = function() {
            planner.drop.ui.remove_selections();
            
            if ( planner.roster.ui.is_visible() ) {
                planner.roster.ui.disable_buttons_that_need_selection();
            }
            
            if ( planner.garage.ui.is_visible() ) {
                planner.garage.ui.disable_buttons_that_need_selection();
            }
        };
        
        $('body').click(remove_ui_selections);
        $('body').dblclick(remove_ui_selections);
    }
});